package edu.xpu.project.analysis.controller;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import edu.xpu.project.analysis.model.ClassAvgModel;
import edu.xpu.project.analysis.model.ScoreAnalysisModel;
import edu.xpu.project.analysis.model.ScoreDistributeModel;
import edu.xpu.project.analysis.service.SingleScoreAnalysisService;
import edu.xpu.project.common.utils.ConfigReader;
import edu.xpu.project.user.entity.CourseType;
import edu.xpu.project.user.service.CourseTypeService;


/**
 * @describe 成绩分析controller入口
 * 			单科成绩分析
 * @author liukang
 * @date 2017年5月28日
 */
@Controller
@RequestMapping(value="analysisSingle")
public class ScoreAnalysisController {
	@Autowired
	private CourseTypeService courseTypeService;
	@Autowired
	private SingleScoreAnalysisService analysisService;
	
	@RequestMapping(value="singleCourseScore/{courseName}")
	public String singleCourseScore(@PathVariable(value="courseName") String courseName, ModelMap modelMap) {
		CourseType byCourseName = courseTypeService.getByCourseName(courseName);
		if (byCourseName != null) {
			Long courseId = byCourseName.getId();
			//每个班的均分
			List<ClassAvgModel> avgByClasses = analysisService.getAvgByClasses(courseId);
			//优秀率(计算方式)
			Integer perfectScore = 90;
			try {
				perfectScore = Integer.parseInt(ConfigReader.sysPropertiesVal("perfectScore"));
			} catch (NumberFormatException e) {}
			//及格率(计算方式)
			Integer passScore = 60;
			try {
				passScore = Integer.parseInt(ConfigReader.sysPropertiesVal("passScore"));
			} catch (NumberFormatException e) {}
			
			//过差率
			Integer badScore = 30;
			try {
				badScore = Integer.parseInt(ConfigReader.sysPropertiesVal("badScore"));
			} catch (NumberFormatException e) {}
			
			//前10--30共有多少人
			List<ScoreAnalysisModel> analysisModels = new ArrayList<ScoreAnalysisModel>(avgByClasses.size());
			for (ClassAvgModel classAvgModel : avgByClasses) {
				ScoreAnalysisModel scoreAnalysisModel = new ScoreAnalysisModel();
				//1、获取前10
				List<Object> objList10 = analysisService.getScoreListByCourseIdAndClassid(10, classAvgModel.getClassId(), courseId);
				scoreAnalysisModel.setTop10(parseObjList(new ScoreDistributeModel(), objList10));
				//2、获取前20
				List<Object> objList20 = analysisService.getScoreListByCourseIdAndClassid(20, classAvgModel.getClassId(), courseId);
				scoreAnalysisModel.setTop20(parseObjList(new ScoreDistributeModel(), objList20));
				//3、获取前30
				List<Object> objList30 = analysisService.getScoreListByCourseIdAndClassid(30, classAvgModel.getClassId(), courseId);
				scoreAnalysisModel.setTop30(parseObjList(new ScoreDistributeModel(), objList30));
				scoreAnalysisModel.setClassId(classAvgModel.getClassId());
				scoreAnalysisModel.setAvgScore(Double.parseDouble(nf.format(classAvgModel.getClassAvg())));
				scoreAnalysisModel.setClassName(classAvgModel.getClassName());
				
				int totalNum = analysisService.getScoreTotalByCauseIdAndClassId(courseId, classAvgModel.getClassId());
				//获取优秀率
				int perfectNum = analysisService.getScoreTotalByCauseIdAndClassIdForGT(courseId, classAvgModel.getClassId(), perfectScore);
				scoreAnalysisModel.setPerfectPercent(getPrecent(totalNum, perfectNum)*100);
				//获取及格率
				Integer passNum = analysisService.getScoreTotalByCauseIdAndClassIdForGT(courseId, classAvgModel.getClassId(), passScore);
				scoreAnalysisModel.setPassPercent(getPrecent(totalNum, passNum)*100);
				//获取过差率
				Integer badNum = analysisService.getScoreTotalByCauseIdAndClassIdForLT(courseId, classAvgModel.getClassId(), badScore);
				scoreAnalysisModel.setBadPercent(getPrecent(totalNum, badNum)*100);
				
				analysisModels.add(scoreAnalysisModel);
			}
			//处理数据返回
			modelMap.addAttribute("analysisModels", analysisModels);
		}
		modelMap.addAttribute("courseName", courseName);
		return "analysis/singleScoreShow";
	}
	
	private static NumberFormat nf = NumberFormat.getNumberInstance();
	static {
		nf.setMaximumFractionDigits(4);
	}
	public Double getPrecent(Integer totalScore, Integer partScore) {
		Double precent = (double) partScore / totalScore;
		String format = nf.format(precent);
		return Double.parseDouble(format);
	}

	private ScoreDistributeModel parseObjList(ScoreDistributeModel scoreDistributeModel, List<Object> objList) {
		for (Object object : objList) {
			Object[] objArr = (Object[]) object;
			Double doubVal = (Double) objArr[1];
			if (doubVal > 110 && doubVal <= 120) {
				scoreDistributeModel.setD120to110(scoreDistributeModel.getD120to110() + 1);
			} else if (doubVal > 100 && doubVal <= 110) {
				scoreDistributeModel.setD110to100(scoreDistributeModel.getD110to100() + 1);
			} else if(doubVal > 90 && doubVal <= 100) {
				scoreDistributeModel.setD100to90(scoreDistributeModel.getD100to90() + 1);
			}else if(doubVal > 80 && doubVal <= 90) {
				scoreDistributeModel.setD90to80(scoreDistributeModel.getD90to80() + 1);
			}else if(doubVal <= 80) {
				scoreDistributeModel.setD80Down(scoreDistributeModel.getD80Down() + 1);
			}
		}
		return scoreDistributeModel;
	}

}
